package com.hddz.acquisition.activity.stock;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hddz.acquisition.R;
import com.hddz.acquisition.activity.BaseActivity;
import com.hddz.acquisition.fragment.stock.FragmentGroundingFree1;
import com.hddz.acquisition.fragment.stock.FragmentGroundingScan;
import com.hddz.acquisition.model.JsonRespose;
import com.hddz.acquisition.view.XEditText;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_stock_grounding)
public class PruductOutStockActivity extends BaseActivity {
    private final String TAG = "PruductOutStockActivity";
    @ViewInject(R.id.tab_vpager)
    private ViewPager tab_vpager;
    @ViewInject(R.id.lbl_main_title)
    private TextView lbl_main_title;
    @SuppressLint("UseSparseArrays")
    private Map<Integer, Fragment> mCacheFragments = new HashMap<Integer, Fragment>();
    private Fragment mFm = null;
    FragmentGroundingScan fragmentGroundingScan = null;
    FragmentGroundingFree1 fragmentGroundingFree1 = null;

    String price = "";


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lbl_main_title.setText(R.string.menu_bpck);
        tab_vpager.setAdapter(new MainViewPagerAdapter(
                getSupportFragmentManager()));
        tab_vpager.setOffscreenPageLimit(2);

        baseSendCommand("1701", "", price);
    }


    private class MainViewPagerAdapter extends FragmentPagerAdapter {

        public MainViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
            if (mCacheFragments.containsKey(position)) {
                mFm = mCacheFragments.get(position);
            } else {
                switch (position) {
                    case 0:
                        fragmentGroundingScan = FragmentGroundingScan.newInstance();
                        mFm = fragmentGroundingScan;
                        break;
                    case 1:
                        fragmentGroundingFree1 = FragmentGroundingFree1
                                .newInstance();
                        mFm = fragmentGroundingFree1;
                        break;
                    default:
                        break;
                }
                mCacheFragments.put(position, mFm);
            }
            return mFm;
        }
    }


    @Event(R.id.btn_back)
    private void onBack(View view) {
        if (targetwarehouse.equals("")) {
            finish();
        } else {

            dialogHelper.showAlertDialog("是否取消操作?", 24, new OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    baseSendCommand("1705", "", "", targetwarehouse, price);
                }
            });
        }
    }

    @Event(R.id.btn_finish)
    private void onFinish(View view) {
        if (targetwarehouse.equals("")) {
            dialogHelper.showAlertDialog("未扫描架子！", 24);
        } else {
            baseSendCommand("1704", "", "", targetwarehouse, price);
        }
    }

    XEditText edit;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getAction() == KeyEvent.ACTION_UP) {
            Log.d("Key", "Enter Key:" + event.getKeyCode());
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_F1:
                    fragmentGroundingScan.cb_unActive.setChecked(true);
                    return true;
                case KeyEvent.KEYCODE_F4:
                    LayoutInflater factory = LayoutInflater.from(PruductOutStockActivity.this);//提示框
                    final View view = factory.inflate(R.layout.dialog_price_input, null);//这里必须是final的
                    edit = (XEditText) view.findViewById(R.id.et_price);//获得输入框对象
                    edit.setMaxmumFilter(100.0, 2);

                    new AlertDialog.Builder(PruductOutStockActivity.this)
                            .setTitle("输入0-100的单价")//提示框标题
                            .setView(view)
                            .setPositiveButton("确定",//提示框的两个按钮
                                    new android.content.DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                            int which) {
                                            price = edit.getText().toString();
                                            fragmentGroundingScan.updatePrice(edit.getText().toString());
                                            edit = null;
                                        }
                                    }).setNegativeButton("取消", null).create().show();
                    return true;
                case KeyEvent.KEYCODE_STAR:
                case KeyEvent.KEYCODE_POUND:
                case KeyEvent.KEYCODE_PERIOD:
                case KeyEvent.KEYCODE_NUMPAD_DOT:
                    if (edit != null) {
                        edit.setText(edit.getText() + ".");
                    }
                    return true;
                default:
                    break;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void addResponse(JsonRespose json) {
        if (currentCmd.equals("1701")) {

        } else if (currentCmd.equals("1702")) {
            targetwarehouse = json.response.cTargetWarehouse;
            fragmentGroundingScan.updateResponse(json);
        } else if (currentCmd.equals("1703")) {
            fragmentGroundingScan.updateResponse(json);
            fragmentGroundingFree1
                    .updateList(json.response.ResponseList);
        } else if (currentCmd.equals("1704")) {
            Toast.makeText(PruductOutStockActivity.this, "保存成功!",
                    Toast.LENGTH_SHORT).show();
            finish();
        } else if (currentCmd.equals("1705")) {
            finish();
        }
        fragmentGroundingScan.appandMsg(json.message);
    }

    @Override
    public void addErr(String err) {
        fragmentGroundingScan.appandMsg(err);
    }

    @Override
    public void receiveCode(String code) {
        fragmentGroundingScan.updateBarcode(code);
        if (currentCmd.equals("1701")) {
            baseSendCommand("1702", "", "", code, price);
        } else if (currentCmd.equals("1702")
                || currentCmd.equals("1703")
                || currentCmd.equals("1706")) {
            if (fragmentGroundingScan.cb_unActive.isChecked()) {
                fragmentGroundingScan.cb_unActive.setChecked(false);
                baseSendCommand("1706", code, "", targetwarehouse, price);
            } else {
                baseSendCommand("1703", code, "", targetwarehouse, price);
            }
        }
    }

    @Override
    public void backDo() {
        onBack(null);
    }

    @Override
    public void finishDo() {
        onFinish(null);
    }
}
